﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using vJine.Core;
using vJine.Lua.GUI.BLL;
using vJine.Lua.GUI.UI;

namespace vJine.Lua.GUI {
    /// <summary>
    /// WinGUI.xaml 的交互逻辑
    /// </summary>
    public partial class WinGUI : Window {
        public WinGUI() {
            InitializeComponent();
        }
        public static void load(AppGUI appGUI, string gui_path, Window parent, LuaContext context, NodeConfig nConfig) {
            UserControl uCtrl = load_xaml_content(gui_path);
            double guiWith = uCtrl.Width + 10;
            double guiHeight = uCtrl.Height + 172;
            WinGUI winGui = new WinGUI(){
                WindowStartupLocation = WindowStartupLocation.CenterOwner, ShowInTaskbar = false, ShowActivated = true,
                luaContext = context, luaConfig = nConfig,
                MinWidth = guiWith, Width = guiWith, MinHeight = guiHeight, Height = guiHeight
            };
            appGUI.SwitchPresentor(winGui.rtbMessage, winGui.Dispatcher);
            
            winGui.Owner = parent;
            winGui.UseLayoutRounding = true;
                
            uCtrl.Loaded += winGui.uCtrl_Loaded;
            uCtrl.Margin = new Thickness(0);
            winGui.M.Children.Add(uCtrl);
            winGui.UpdateLayout();

            winGui.ShowDialog();
        }

        LuaContext luaContext;
        NodeConfig luaConfig;

        AppCmdProxy cmdProxy = null;
        void uCtrl_Loaded(object sender, RoutedEventArgs e) {
            this.cmdProxy = new AppCmdProxy((DependencyObject)sender, this.rtbMessage, this.luaContext, this.luaConfig, (double pM, double pS) => {
                this.Dispatcher.BeginInvoke(new Action(() => {
                    this.uctProgress.ProgMain = pM;
                    this.uctProgress.ProgSub = pS;
                }));
            }, (NodeConfig cmd) => {
                btnRun.IsEnabled = true;
                btnCancel.IsEnabled = true;
                btnIgnore.IsEnabled = true;
                {
                    this.DataContext = cmd;
                    btnRun.Command = cmd;
                    btnRun.SetBinding(Button.ContentProperty, "cmd_run");
                    btnRun.SetBinding(Button.CommandParameterProperty, "cmd_run_param");
                }
                
                btnCancel.Command = cmd;
                btnIgnore.Command = cmd;
            },
                (NodeConfig cmd) => {
                    btnRun.IsEnabled = false;
                    btnCancel.IsEnabled = false;
                    btnIgnore.IsEnabled = false;
                });
            this.UpdateLayout();
        }

        static UserControl load_xaml_content(string file_name) {
            using (FileStream fs = new FileStream(file_name, FileMode.Open)) {
                UserControl uCtrl = (UserControl)XamlReader.Load(fs);
                return uCtrl;
            }
        }        
    }
}
